<?php

namespace app\common\service;


class HostPort extends Base 

{
    /**
     * @name: 端口列表
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-04-09 10:49:26
     */
    function list ($filterData = [], $limit)
    {
        $server = db('HostPort')->where('host_id', $filterData['host_id']);
        //条件查询
        if (isset($filterData['port']) && !empty($filterData['port'])) {
            $server->where('port','like', '%'.$filterData['port'].'%') ;
        }
        if(isset($filterData['protocols']) && is_numeric($filterData['protocols'])){
            $server->where('protocols', $filterData['protocols']);
        }
        if(isset($filterData['guise']) && is_numeric($filterData['guise'])){
            //0为自开放端口,1为伪装端口
            $server->where('guise', $filterData['guise']);
        }
        if(isset($filterData['status']) && is_numeric($filterData['status'])){
            //0删除,1正常
            $server->where('status', $filterData['status']);
        }

        $return = $server->paginate($limit)->toArray();

        return $return;
    }

    /**
     * @name: 批量添加
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-07 14:25:16
     */
    function addMore ($data)
    {
        db('HostPort')->insertAll($data);
    }

    /**
     * @name: 
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-28 16:19:02
     */
    function checkPort ($hostPort_a)
    {
        $conflict = [];
        foreach ($hostPort_a as $host_id => $port)
        {
            $port_a = Model('HostPort')->whereIn('port', $port)->where('host_id', $host_id)->where('guise', 0)->count();
            if ($port_a)
            {
                $conflict[]  = $host_id;
            }
        }
        
        if ($conflict)
        {
            $this->setError($this->CODE_FAIL, $this->hostErrorMessage($conflict, '端口冲突'));
            return false;
        }
        
    }

    /**
     * @name: 删除主机 通过 主机id
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-31 14:30:20
     */
    function delByHostId ($host_id_a)
    {
        db('HostPort')->whereIn('host_id', $host_id_a)->delete();
    }
}